package demo4.controller;

import demo4.mapper.IUserMapper;
import demo4.mapper.UserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.util.Scanner;

/**
 * @author 李明洋
 * @date 2025/3/28 16:36
 * @version: 1.0
 */
@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;

    public void start() {
        System.out.println("输入以下序列号会弹出对应的功能");
        System.out.println("1,用户注册 2,用户登录 3,再说");
        Scanner scanner = new Scanner(System.in);
        int num = Integer.parseInt(scanner.next());
        switch (num) {
            case 1:
                regUser();
                break;
            case 2:
                loginUser();
                break;
            default:
                System.out.println("别急别急");
        }
    }

    public void regUser() {
        System.out.println("欢迎注册");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        System.out.println("请输入昵称");
        String nickname = scanner.nextLine();
        System.out.println("请输入年龄");
        int age = scanner.nextInt();

        if (userMapper.selectByUsername(username) != null) {
            System.out.println("该用户已存在");
        } else {
            User user = new User(null, username, password, nickname, age);
            int num = userMapper.insert(user);
            System.out.println(num > 0 ? "注册成功" : "注册失败");
        }
    }

    public void loginUser() {
        System.out.println("开始处理用户登录");
        System.out.println("输入账号");
        Scanner sc = new Scanner(System.in);
        String username = sc.nextLine();
        System.out.println("输入密码");
        String password = sc.nextLine();
        if (userMapper.selectByUsername(username).getUsername().equals(username) &&
                userMapper.selectByUsername(username).getPassword().equals(password)

        ) {
            System.out.println("true");
        }else {
            System.out.println("false");
        }

    }
}
